package com.pearadmin.pro.modules.exam.controller;

import com.pearadmin.pro.common.aop.annotation.Log;
import com.pearadmin.pro.common.web.domain.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pearadmin.pro.common.constant.ControllerConstant;
import com.pearadmin.pro.common.web.base.BaseController;
import com.pearadmin.pro.modules.exam.dto.ExamDTO;
import com.pearadmin.pro.modules.exam.dto.request.ExamSaveReqDTO;
import com.pearadmin.pro.modules.exam.dto.response.ExamOnlineRespDTO;
import com.pearadmin.pro.modules.exam.service.ExamService;
import com.pearadmin.pro.modules.examUtils.PagingReqDTO;
import com.pearadmin.pro.modules.examUtils.dto.BaseIdReqDTO;
import com.pearadmin.pro.modules.examUtils.dto.BaseIdsReqDTO;
import com.pearadmin.pro.modules.paper.service.UserExamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;


/**
 * 考试控制器
 *
 * @author ZhouXingYun
 * @date 2022/4/24 11:36
 */
@Api(tags = {"考试模块"})
@RestController
@RequestMapping(ControllerConstant.PREFIX_SYS + "exam")
public class ExamController extends BaseController {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");


    @Resource
    private ExamService baseService;
    @Resource
    private UserExamService userExamService;

    @ApiOperation(value = "考试视角")
    @Log(title = "考试视角")
    @RequestMapping(value = "/online-paging", method = {RequestMethod.GET})
    public Result myPaging( PagingReqDTO<ExamDTO> reqDTO) {
        //分页查询并转换
        IPage<ExamOnlineRespDTO> page = baseService.onlinePaging(reqDTO);
        List<ExamOnlineRespDTO> records = page.getRecords();
        records.forEach(examOnlineRespDTO -> {
            System.out.println("examOnlineRespDTO++"+examOnlineRespDTO.getTimeLimit());
            if(examOnlineRespDTO.getTimeLimit()){
                examOnlineRespDTO.setTimeList(dateFormat.format(examOnlineRespDTO.getStartTime())+"~"+dateFormat.format(examOnlineRespDTO.getEndTime()));
            }else {
                examOnlineRespDTO.setTimeList("不限时");
            }
        });
        return success(page);
    }


    /**
     * 查找详情
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找详情")
    @Log(title = "查找详情")
    @RequestMapping(value = "/detail", method = { RequestMethod.POST})
    public Result<ExamSaveReqDTO> find(@RequestBody BaseIdReqDTO reqDTO) {
        ExamSaveReqDTO dto = baseService.findDetail(reqDTO.getId());
        return success(dto);
    }
    /**
     * 分页查找试卷
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找试卷")
    @Log(title = "分页查找试卷")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public Result<IPage<ExamDTO>> paging(@RequestBody PagingReqDTO<ExamDTO> reqDTO) {

        //分页查询并转换
        IPage<ExamDTO> page = baseService.paging(reqDTO);
        List<ExamDTO> records = page.getRecords();
        records.forEach(examOnlineRespDTO -> {
            System.out.println("examOnlineRespDTO++"+examOnlineRespDTO.getTimeLimit());
            if(examOnlineRespDTO.getTimeLimit()){
                examOnlineRespDTO.setTimeList(dateFormat.format(examOnlineRespDTO.getStartTime())+"~"+dateFormat.format(examOnlineRespDTO.getEndTime()));
            }else {
                examOnlineRespDTO.setTimeList("不限时");
            }
        });
        return success(page);
    }


    /**
     * 添加或修改试卷
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "添加或修改试卷")
    @Log(title = "添加或修改试卷")
    @RequestMapping(value = "/save", method = { RequestMethod.POST})
    public Result save(@RequestBody ExamSaveReqDTO reqDTO) {
        //复制参数
        baseService.save(reqDTO);
        return success();
    }
    /**
     * 批量删除
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "批量删除试卷")
    @Log(title = "批量删除试卷")
    @RequestMapping(value = "/delete", method = { RequestMethod.DELETE})
    public Result edit(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        baseService.removeByIds(reqDTO.getIds());
        userExamService.deleteByExam(reqDTO.getIds());
        return success();
    }

}
